<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  website
 */

class m_website_actions_block_modal_SwitchPageAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
        $page = $this->getDocumentFromRequest();
        $pageInConflict = f_document_Provider::getInstance()->getByModelAndUniqueId('website/Page', $this->getRequestParameter('pageId'), $this->getRequestParameter('pageLang'));

        if($this->isFormSubmit())
        {
            $formValues = $this->getFormValues('form');

            switch ($formValues['action'])
            {
                case '1':
                    // Unpublish page in conflict, publish this one
                    $pageInConflict->setPublicationstatus('DRAFT')->save(false);
                    $page->setPublicationstatus('PUBLISHED')->save(false);
                    $this->closeModalAndRefresh();
                break;

                case '2':
                    // Delete page in conflict, publish this one
                    $pageInConflict->delete();
                    $page->setPublicationstatus('PUBLISHED')->save(false);
                    $this->closeModalAndRefresh();
                break;

                case '3':
                    // Set a new url to the current page
                    if(isset($formValues['url']) && $formValues['url'] != '')
                    {
                        if($formValues['url'] != $page->getFormattedUrl())
                        {
                            $page->setUrl('/'.$formValues['url']);

                            if(!$this->checkUrl($page))
                            {
                                $page->setPublicationStatus('PUBLISHED')->save(false);
                                $this->closeModalAndRefresh();
                            }
                            else
                            {
                                $this->addToModel('error', $this->getLocale('website.errors.duplicatePageURL2'));
                            }
                        }
                        else
                        {
                            $this->addToModel('error', $this->getLocale('website.errors.urlIsTheSame'));
                        }
                    }
                    else
                    {
                        $this->addToModel('error', $this->getLocale('website.errors.required'));
                    }
                break;

                default:
                    // Return error
                break;
            }
        }

        $this->addToModel('pageInConflict', $pageInConflict);
        $this->addToModel('currentPage', $page);
        $this->setModalParameters();
        $this->setView('success');
	}

/**
	 * Check if url exist
	 * @return boolean
	 */
	private function checkUrl($page)
	{
        $this->conflictPage =    $this->getDocumentProvider()->loadDocuments("modules_website/Page")
                            ->whereIsEqual("document_publicationstatus", "PUBLISHED")
                            ->whereIsEqual("url", $page->getUrl())
                            ->whereIsEqual("websiteId", $page->getWebsiteId())
                            ->retrieveFirstDocument($page->getLang());

        return count($this->conflictPage) > 0;
	}
}